unit mapinfolist;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  fpjson, jsonparser;

type

  { TMapInfoList }

  TMapInfoList = class
    private
      Filename: String;
    public
      Data: TJSONArray;
      procedure Open(AFilename: String);
      procedure Save;
      function GetMapName(MapId: Integer): String;
  end;

implementation

procedure TMapInfoList.Open(AFilename: String);
var
  Stream: TFileStream;
  Parser: TJSONParser;
begin
  Filename := AFilename;
  try
    Stream := TFileStream.Create(AFilename, fmOpenRead);
    Parser := TJSONParser.Create(Stream);
    try
      Data := TJSONArray(Parser.Parse);
    finally
      Parser.Free;
    end;
  finally
    Stream.Free;
  end;
end;

procedure TMapInfoList.Save;
begin
end;

function TMapInfoList.GetMapName(MapId: Integer): String;
var
  Name: String = '';
begin
  if (MapId >= 1) and (MapId < Data.Count) then begin
    Name := Trim(Data.Objects[MapId].Strings['name']);
  end;

  if Name = '' then
    Name := Format('%0.3d',[MapId]);

  GetMapName := Name;
end;

end.

